﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Security.Permissions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AIStudio.Wpf.ECharts.Controls
{
    /// <summary>
    /// CandlestickEchart.xaml 的交互逻辑
    /// </summary>
    public partial class CandlestickEchart : UserControl
    {
        public static readonly DependencyProperty ValuesCollectionProperty = DependencyProperty.Register(
               "ValuesCollection", typeof(ObservableCollection<List<object>>), typeof(CandlestickEchart), new PropertyMetadata(null, OnValuesCollectionPropertyCallback));
        public ObservableCollection<List<object>> ValuesCollection
        {
            get { return (ObservableCollection<List<object>>)GetValue(ValuesCollectionProperty); }
            set { SetValue(ValuesCollectionProperty, value); }
        }

        private static void OnValuesCollectionPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var chart = (CandlestickEchart)d;
            chart.OnValuesCollectionPropertyCallback(e.NewValue as ObservableCollection<LabelClass>, e.OldValue as ObservableCollection<LabelClass>);
        }

        private void OnValuesCollectionPropertyCallback(ObservableCollection<LabelClass> newvalue, ObservableCollection<LabelClass> oldvalue)
        {
            if (oldvalue != null)
            {
                oldvalue.CollectionChanged -= CandlestickEchart_CollectionChanged;
            }
            if (newvalue != null)
            {
                newvalue.CollectionChanged += CandlestickEchart_CollectionChanged;
            }
            DrawLine();
        }

        private void CandlestickEchart_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            DrawLine();
        }

        public CandlestickEchart()
        {
            InitializeComponent();
            Web.ObjectForScripting = new WebAdapter(Web);
            Web.Navigate(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "Htmls\\CandlestickChart.html"));
            SizeChanged += CandlestickEchart_SizeChanged;

            this.Loaded += CandlestickEchart_Loaded;
        }

        private void CandlestickEchart_Loaded(object sender, RoutedEventArgs e)
        {
            Task.Run(async delegate
            {
                await Task.Delay(1000);

                Dispatcher.Invoke(() => DrawLine());
            });

        }

        private void CandlestickEchart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (!IsLoaded) return;

            try
            {
                Web.InvokeScript("jsSetSize", (int)(this.ActualWidth * 0.97), (int)(this.ActualHeight * 0.97));
                DrawLine();
            }
            catch { }
        }

        private void DrawLine()
        {
            try
            {
                if (!IsLoaded) return;

                if (ValuesCollection != null)
                {
                    Web.InvokeScript("jsSetDatas", Newtonsoft.Json.JsonConvert.SerializeObject(ValuesCollection));
                }
            }
            catch (Exception ex)
            {

            }
        }


        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        [System.Runtime.InteropServices.ComVisible(true)]//给予权限并设置可见
        public class WebAdapter
        {
            WebBrowser web;
            public WebAdapter(WebBrowser web)
            {
                this.web = web;
            }
            public void ShowMsg(string Msg)
            {
                MessageBox.Show(Msg);
            }
        }
    }

}
